package com.zlzxm.shoesmarket.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * CREATE BY zlzxm ON 4/22/21
 */
public class CircleProgressView extends View {


    private int width;
    private int height;
    private RectF rectF = new RectF();
    private Paint pOne = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint pTwo = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint pThree = new Paint(Paint.ANTI_ALIAS_FLAG);
    private  Path pathOne = new Path();
    private  Path pathTwo = new Path();
    private  Path pathThree = new Path();

    private int strokeOne = 10;
    private int strokeTwo = 20;
    private int strokeThree = 30;

    public CircleProgressView(Context context) {
        this(context,null);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){

        Handler handler = new Handler();


        pOne.setStyle(Paint.Style.STROKE);
        pOne.setColor(Color.parseColor("#D85A2C"));
        pOne.setStrokeWidth(strokeOne);
        pOne.setStrokeCap(Paint.Cap.ROUND);

        pTwo.setStyle(Paint.Style.STROKE);
        pTwo.setColor(Color.parseColor("#FFA32B"));
        pTwo.setStrokeWidth(strokeTwo);
        pTwo.setStrokeCap(Paint.Cap.ROUND);

        pThree.setStyle(Paint.Style.STROKE);
        pThree.setColor(Color.parseColor("#EBDA43"));
        pThree.setStrokeWidth(strokeThree);
        pThree.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        rectF.set(strokeThree /2 ,strokeThree / 2,w-strokeThree /2,w - strokeThree /2 );

        double radius =  w /  2.0  - strokeThree  / 2.0;

        float start = (float) Math.toDegrees(Math.acos( (h -radius) /radius)) + 10;

        pathOne.addArc(rectF,start + 90,360 - start * 2);
        pathTwo.addArc(rectF,start + 90,360 - start * 4);
        pathThree.addArc(rectF,start + 90,360 - start * 6);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(pathOne,pOne);

        canvas.drawPath(pathTwo,pTwo);

        canvas.drawPath(pathThree,pThree);


    }
}
